package org.firecloud.h5fun.model;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;

import org.json.JSONArray;
import org.json.JSONObject;

import com.mchange.v2.c3p0.ComboPooledDataSource;
/**
 * 网站配置类与WEB-INF/config.json对应<br>
 * @author fc
 */
public class CommonConfig {
	private String name = null;
	private String title = null;
	private String email = null;
	private String mysqlUrl = null;
	private String mysqlUserName = null;
	private String mysqlPassWord = null;
	private String[] orderByNames = null;
	private String[] orderByTypes = null;
	private int mainPageSize = 0;
	private int searchPageSize = 0;
	private int userInfoAppsSize = 0;
	private int commentaryPageSize = 0;
	private String tulingApikey = null;
	private ComboPooledDataSource cpds = null;
	private String homeUrl = null;
	
	
	/**
	 * @return 评论每页数量
	 */
	public int getCommentaryPageSize() {
		return commentaryPageSize;
	}
	/**
	 * @return 用户信息页每页App数量
	 */
	public int getUserInfoAppsSize() {
		return userInfoAppsSize;
	}
	/**
	 * @return 主页URL
	 */
	public String getHomeUrl() {
		return homeUrl;
	}
	/**
	 * @return 图灵机器人ApiKey
	 */
	public String getTulingApikey() {
		return tulingApikey;
	}
	/**
	 * @return 网站名字
	 */
	public String getName() {
		return name;
	}
	/**
	 * @return 网站标题
	 */
	public String getTitle() {
		return title;
	}
	/**
	 * @return 网站管理邮箱
	 */
	public String getEmail() {
		return email;
	}
	/**
	 * @return 排序根据名字数组
	 */
	public String[] getOrderByNames() {
		return orderByNames;
	}
	/**
	 * @return 排序方法名字数组
	 */
	public String[] getOrderByTypes() {
		return orderByTypes;
	}
	/**
	 * @return 主页每项目显示数量
	 */
	public int getMainPageSize() {
		return mainPageSize;
	}
	/**
	 * @return 搜索每页数量
	 */
	public int getSearchPageSize() {
		return searchPageSize;
	}
	
	/**
	 * 从数据源获取数据库连接
	 * @return 数据库连接
	 * @throws SQLException 数据源错误
	 */
	public Connection getConn() throws SQLException {
		return cpds.getConnection();
	}
	
	/**
	 * @param config，config.json解析后内容
	 * @throws SQLException 构造数据源错误
	 * @throws PropertyVetoException jdbc驱动ClassName错误
	 */
	public CommonConfig(JSONObject config) throws SQLException, PropertyVetoException {
		name = config.getString("name");
		title = config.getString("title");
		email = config.getString("email");
		mysqlUrl = config.getString("mysqlUrl");
		mysqlUserName = config.getString("mysqlUserName");
		mysqlPassWord = config.getString("mysqlPassWord");
		tulingApikey = config.getString("tulingApikey");
		mainPageSize = config.getInt("mainPageSize");
		searchPageSize = config.getInt("searchPageSize");
		userInfoAppsSize = config.getInt("userInfoAppsSize");
		commentaryPageSize = config.getInt("commentaryPageSize");
		
		JSONArray names = config.getJSONArray("orderByNames");
		orderByNames = new String[names.length()];
		for (int i = 0; i < names.length(); ++i) {
			orderByNames[i] = names.getString(i);
		}
		JSONArray types = config.getJSONArray("orderByType");
		orderByTypes = new String[types.length()];
		for (int i = 0; i < types.length(); ++i) {
			orderByTypes[i] = types.getString(i);
		}
		homeUrl = config.getString("homeUrl");
		
		cpds = new ComboPooledDataSource();
		cpds.setDriverClass("org.mariadb.jdbc.Driver");
		cpds.setJdbcUrl(mysqlUrl);
		cpds.setUser(mysqlUserName);
		cpds.setPassword(mysqlPassWord);
	}
}